import router from './routes'
import store from '@/store'
import { generateRouter } from '@/utils/loadRoutes'
import { getToken } from '@/utils/auth'

// 前置路由守卫
/* router.beforeEach(async (to, from, next) => {
    // 添加动态路由
    // 用户登录了，且用户动态路由数组为空时
    console.log('执行了！！！');
    console.log(store.getters.auth);
    console.log(store.getters.routes.length);
    if (store.getters.auth && store.getters.routes.length == 0) {
        console.log('添加路由了！！！！');
        await store.dispatch('setUserRoutes');
        console.log('生成路由前====', store.getters.routes);
        const newRouters = generateRouter(store.getters.routes);
        console.log(newRouters);
        router.addRoutes(newRouters)
        console.log('do00000000000000', store.getters.routes);
        next({ path: to.path })

    } else {
        next()
    }
}) */


router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    if (getToken()) {
        console.log("登录了，再次进入路由守卫");
        // 已登录且要跳转的页面是登录页
        if (to.path === '/login') {
            next({ path: '/' })
        } else {
            // 用户信息为空，管理员角色信息不为空
            if (Object.keys(store.getters.user).length === 0 && Object.keys(store.getters.role).length === 0) {
                store.dispatch('getUserInfo').then(() => {
                    loadMenus(to, next);
                }).catch(() => {
                    //   TODO:
                    store.dispatch('logout').then(() => {
                        location.reload() // 为了重新实例化vue-router对象 避免bug
                    })
                })
            }
            else if (Object.keys(store.getters.user).length === 0 && Object.keys(store.getters.role).length !== 0) { // 判断当前用户是否为空，未拉取完user_info信息
                store.dispatch('getUserInfo').then(() => {
                    // 动态路由，拉取菜单并放行
                    /* store.dispatch('setUserRoutes');
                    next(); */
                    loadMenus(to, next);
                }).catch(() => {
                    //   TODO:
                    store.dispatch('logout').then(() => {
                        location.reload() // 为了重新实例化vue-router对象 避免bug
                    })
                })
                // 用户信息不为空，管理员角色信息为空
            } else if (Object.keys(store.getters.user).length !== 0 && Object.keys(store.getters.role).length === 0) {
                console.log("+++++++000000000+++++进来了");
                store.dispatch('getUserRole').then(() => {
                    // 动态路由，拉取菜单并放行
                    /* store.dispatch('setUserRoutes');
                    next(); */
                    loadMenus(to, next);
                }).catch(() => {
                    console.log("拉取不到管理员角色");
                    store.dispatch('logout').then(() => {
                        location.reload() // 为了重新实例化vue-router对象 避免bug
                    })
                })
            }
            else if (store.getters.loadMenus) {
                // 修改成false，防止死循环
                store.dispatch('updateLoadMenus')
                // 拉取动态路由
                store.dispatch('setUserRoutes');
            } else {
                next();
            }
        }
    }
    // localstorage没有token
    else if (to.path !== '/login') {
        // 排除要跳转的页面是登录页，避免死循环
        next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
    } else {
        // 未登录且要前往的页面是login页面
        next()
    }
})

function loadMenus(to, next) {
    store.dispatch('setUserRoutes').then(() => {
        // store里已经拉取路由，在这生成动态路由表
        const newRouters = generateRouter(store.getters.routes);
        console.log(newRouters);
        router.addRoutes(newRouters)
        console.log('do00000000000000', store.getters.routes);
        next({ path: to.path })
    })
}